/**
 * 
 */
package GUI;

import java.awt.*;

import javax.swing.*;

import Entity.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * @author aviat
 *
 */
public class removeWorker extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel lblWorkerId;
	private JButton btnRemove;
	private JComboBox<String> uIDcomboBox;
	private JLabel label;
	private JComboBox<String> NameComboBox;
	private JComboBox<String> LastNameComboBox;
	private JButton btnRefresh;
	
	private ArrayList<User> m_allUsers;
	private ArrayList<User> m_filteredUsers;
	private Object syncObject;
	private boolean m_isChangeAllowed;
	private JLabel lblWorkerAddress;
	private JComboBox<String> WorkerAddresscomboBox;
	
	public removeWorker() {
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(removeWorker.class.getResource("/resources/back22.png")));
		btnRemove = new JButton("Remove");
		
		btnRemove.setIcon(new ImageIcon(removeWorker.class.getResource("/resources/delete.png")));
		
		uIDcomboBox = new JComboBox<String>();
		AutoCompletion.enable(this.uIDcomboBox);
		uIDcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(uIDcomboBox.getSelectedItem().toString(),"UNAME");
				}
			}
		});
		NameComboBox = new JComboBox<String>();
		AutoCompletion.enable(this.NameComboBox);
		NameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(NameComboBox.getSelectedItem().toString(),"FNAME");
				}
			}
		});
		
		WorkerAddresscomboBox = new JComboBox<String>();
		
		LastNameComboBox = new JComboBox<String>();
		AutoCompletion.enable(this.LastNameComboBox);
		LastNameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(LastNameComboBox.getSelectedItem().toString(),"LNAME");
				}
			}
		});
		btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					filterList(null,"ALL");
					}
			}
		});
		
		initialize();
	}

	private void initialize() {


		JLabel lblRemoveWorker = new JLabel("Remove Worker");
		lblRemoveWorker.setForeground(new Color(0, 204, 255));
		lblRemoveWorker.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblRemoveWorker.setBounds(10, 11, 139, 44);
		add(lblRemoveWorker);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		lblWorkerId = new JLabel("Worker ID");
		lblWorkerId.setForeground(Color.WHITE);
		lblWorkerId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWorkerId.setBounds(190, 257, 101, 27);
		add(lblWorkerId);
		
		uIDcomboBox.setBounds(301, 258, 139, 27);
		add(uIDcomboBox);
		
		btnRemove.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnRemove.setBounds(311, 399, 124, 27);
		add(btnRemove);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		JLabel lblName = new JLabel("First Name:");
		lblName.setForeground(Color.WHITE);
		lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblName.setBounds(190, 122, 101, 27);
		add(lblName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setForeground(Color.WHITE);
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblLastName.setBounds(190, 188, 101, 27);
		add(lblLastName);
		
		NameComboBox.setBounds(301, 126, 139, 27);
		add(NameComboBox);
		
		LastNameComboBox.setBounds(301, 192, 139, 27);
		add(LastNameComboBox);
		
		
		btnRefresh.setIcon(new ImageIcon(removeWorker.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.setBounds(487, 192, 124, 27);
		add(btnRefresh);
		
		lblWorkerAddress = new JLabel("Worker Address");
		lblWorkerAddress.setForeground(Color.WHITE);
		lblWorkerAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWorkerAddress.setBounds(190, 338, 101, 27);
		add(lblWorkerAddress);
		
		
		WorkerAddresscomboBox.addActionListener(new ActionListener(){ 
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(WorkerAddresscomboBox.getSelectedItem().toString(),"ADDRESS");
				}
			}
		});
		WorkerAddresscomboBox.setBounds(301, 335, 134, 44);
		add(WorkerAddresscomboBox);
		
		label = new JLabel("");
		label.setIcon(LOGO);
		label.setBounds(0, 0, 786, 500);
		add(label);
	}

	public void setUsers(ArrayList<User> allUsers) {
		m_allUsers = allUsers;		
		m_filteredUsers = new ArrayList<User>();
		syncObject = new Object();
		filterList(null,"ALL");
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			m_filteredUsers.clear();
			
			switch(scope)
			{
			case "FNAME":
				for(User user : m_allUsers)
				{
					if(user.getName().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "LNAME":
				for(User user : m_allUsers)
				{
					if(user.getLName().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "UNAME":
				for(User user : m_allUsers)
				{
					if(String.valueOf(user.getUserName()).equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "ADDRESS":
				for(User user : m_allUsers)
				{
					if(user.getAddress().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "ALL":
				for(User u : m_allUsers )
					if(!(u.getUserName().equals(ScreenManager.getUserLogged().getUserName())))
						m_filteredUsers.add(u);
				break;
			}
		}
		m_isChangeAllowed = false;
		filterComboBox(NameComboBox,"FNAME");
		filterComboBox(LastNameComboBox,"LNAME");
		filterComboBox(uIDcomboBox,"UNAME");
		filterComboBox(WorkerAddresscomboBox,"ADDRESS");
		m_isChangeAllowed = true;

	}
	
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject) {
			
			for(User user : m_filteredUsers)
			{
				switch(scope)
				{
				case "FNAME":
					combo.addItem(user.getName());
					break;
				case "LNAME":
					combo.addItem(user.getLName());
					break;
				case "UNAME":
					combo.addItem(String.valueOf(user.getUserName()));
					break;
				case "ADDRESS":
					combo.addItem(user.getAddress());
					break;
				}
			}
		}
	}
	
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the btnRemove
	 */
	public JButton getBtnRemove() {
		return btnRemove;
	}

	/**
	 * @return the uIDcomboBox
	 */
	public JComboBox<String> getUIDcomboBox() {
		return uIDcomboBox;
	}
	

	/**
	 * @return the uIDcomboBox
	 */
	public JComboBox<String> getuIDcomboBox() {
		return uIDcomboBox;
	}

	/**
	 * @param uIDcomboBox the uIDcomboBox to set
	 */
	public void setuIDcomboBox(JComboBox<String> uIDcomboBox) {
		this.uIDcomboBox = uIDcomboBox;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param btnRemove the btnRemove to set
	 */
	public void setBtnRemove(JButton btnRemove) {
		this.btnRemove = btnRemove;
	}

	/**
	 * @return the nameComboBox
	 */
	public JComboBox<String> getNameComboBox() {
		return NameComboBox;
	}

	/**
	 * @param nameComboBox the nameComboBox to set
	 */
	public void setNameComboBox(JComboBox<String> nameComboBox) {
		NameComboBox = nameComboBox;
	}

	/**
	 * @return the lastNameComboBox
	 */
	public JComboBox<String> getLastNameComboBox() {
		return LastNameComboBox;
	}

	/**
	 * @param lastNameComboBox the lastNameComboBox to set
	 */
	public void setLastNameComboBox(JComboBox<String> lastNameComboBox) {
		LastNameComboBox = lastNameComboBox;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the m_allUsers
	 */
	public ArrayList<User> getM_allUsers() {
		return m_allUsers;
	}

	/**
	 * @param m_allUsers the m_allUsers to set
	 */
	public void setM_allUsers(ArrayList<User> m_allUsers) {
		this.m_allUsers = m_allUsers;
	}

	/**
	 * @return the m_filteredUsers
	 */
	public ArrayList<User> getM_filteredUsers() {
		return m_filteredUsers;
	}

	/**
	 * @param m_filteredUsers the m_filteredUsers to set
	 */
	public void setM_filteredUsers(ArrayList<User> m_filteredUsers) {
		this.m_filteredUsers = m_filteredUsers;
	}

	/**
	 * @return the m_isChangeAllowed
	 */
	public boolean isM_isChangeAllowed() {
		return m_isChangeAllowed;
	}

	/**
	 * @param m_isChangeAllowed the m_isChangeAllowed to set
	 */
	public void setM_isChangeAllowed(boolean m_isChangeAllowed) {
		this.m_isChangeAllowed = m_isChangeAllowed;
	}

	/**
	 * @return the workerAddresscomboBox
	 */
	public JComboBox<String> getWorkerAddresscomboBox() {
		return WorkerAddresscomboBox;
	}

	/**
	 * @param workerAddresscomboBox the workerAddresscomboBox to set
	 */
	public void setWorkerAddresscomboBox(JComboBox<String> workerAddresscomboBox) {
		WorkerAddresscomboBox = workerAddresscomboBox;
	}
}
